package com.lzx.iteam.widget;

import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.media.AudioManager;
import android.media.SoundPool;
import android.media.SoundPool.OnLoadCompleteListener;
import com.lzx.iteam.util.StringUtil;

import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Set;

/**
 * @author zhaoxuyang
 *
 */
public class Mp3Player {

    
    AssetFileDescriptor current;
    
    Context context;
    
    SoundPool sp;
    
    float volume;
    
    HashMap<String, Integer> maps;
    
    public Mp3Player(Context context) {
        this.context = context;
       init();
    }
    
    public void init(){
        if(maps!=null){
            maps.clear();
        }
        if(sp!=null){
            sp.release();
        }
        if(sp==null){
            sp = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
            sp.setOnLoadCompleteListener(new OnLoadCompleteListener() {
                @Override
                public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
                    soundPool.play(sampleId, volume, volume, 1, 0, 1f);
                }
            });
        }
        AudioManager mgr = (AudioManager) context.getSystemService(    
                Context.AUDIO_SERVICE);    
        float streamVolumeCurrent = mgr.getStreamVolume(AudioManager.STREAM_MUSIC);     
        float streamVolumeMax = mgr.getStreamMaxVolume(AudioManager.STREAM_MUSIC);  
        volume = streamVolumeCurrent/streamVolumeMax;
        maps = new HashMap<String, Integer>();
    }
    
    public void clearMap(){
        if(maps != null){
            maps.clear();
        }
    }
    
    public  void setSource(String id,AssetFileDescriptor afd, String path){
        if(!maps.containsKey(id)){
            if(!StringUtil.isEmpty(path)){
                maps.put(id, sp.load(path, 1));
            }else{
                maps.put(id, sp.load(afd, 1));
            }
            /*try {
                afd.close();
            } catch (IOException e) {
                e.printStackTrace();
            }*/
        }else{
            System.out.println("reuse not load");
            play(id);
        }
        
    }
    
    public void play(String id){
        sp.play(maps.get(id), volume, volume, 1, 0, 1f);
    }
    
    
    public void destory(){
        Set<Entry<String, Integer>> entry = maps.entrySet();
        if(sp!=null){
            for(Entry<String, Integer> i:entry){
                if(sp.unload(i.getValue())){
                    System.out.println("unload");
                }else{
                    System.out.println(" not unload");
                    while(true){
                        if(!sp.unload(i.getValue())){
                            System.out.println(" t not unload");
                        }else{
                            System.out.println("t unload");
                            break;
                        }
                    }
                   
                }
            }
        }
        maps.clear();
    }
    
}
